<?php

namespace app\cps\controller;

use app\cps\service\RechargeService;
use think\facade\Request;
use app\common\Common;
use app\common\Error;

class Recharge extends Base
{
    private $service;

    public function __construct(RechargeService $service)
    {
        $this->service = $service;
    }

    // 渲染充值设置列表页面
    public function setList()
    {
        return view();
    }

    // 获取使用平台类型列表
    public function getPlatformType()
    {
        $data = config('enum.platform_type');
        return Common::success($data);
    }

    // 获取展示对象
    public function getPisplayObject()
    {
        $data = config('enum.display_object');
        return Common::success($data);
    }

    // 总后台充值设置列表
    public function rechargeSetList()
    {
        $params = Request::param();

        $data = $this->service->getRechargeSetList($params);

        return Common::success($data);
    }

    // 渲染自定义充值设置列表页面
    public function index()
    {
        return view();
    }

    // 渲染自定义充值设置详情页面
    public function info()
    {
        return view();
    }

    // 自定义充值设置列表
    public function list()
    {
        $params = Request::param();

        $data = $this->service->getCustomRechargeSetList($params, $this->getUserInfo());

        return Common::success($data);
    }

    // 自定义充值设置详情
    public function detail()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Recharge::class)->scene('detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getCustomRechargeSetDetail($params['id']);

        return Common::success($data);
    }

    // 添加自定义充值设置
    public function add()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Recharge::class)->scene('add')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCustomRechargeSet($params, $this->getUserInfo());

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 修改自定义充值设置
    public function edit()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Recharge::class)->scene('edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->editCustomRechargeSet($params, $this->getUserInfo());

        if (!$result || !empty($result['message'])) {
            $message = !empty($result['message']) ? $result['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success();
    }

    // 删除自定义充值设置
    public function delete()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Recharge::class)->scene('delete')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->deleteCustomRechargeSet($params['id']);

        if (!$result) {
            return Common::fail();
        }

        return Common::success();
    }

    // 获取自定义充值设置下拉列表
    public function select()
    {
        $data = $this->service->getCustomRechargeSetSelectList($this->getUserInfo());

        return Common::success($data);
    }

    // 获取充值类型
    public function getRechargeType()
    {
        $params = Request::param();

        if ($params['type'] == 1) {
            $data = config('enum.recharge_type_1');
        } else {
            $data = config('enum.recharge_type_2');
        }

        return Common::success($data);
    }

    // 渲染默认充值数据页面
    public function default()
    {
        return view();
    }

    // 默认充值数据
    public function defaultCharge()
    {
        $params = Request::param();
        $data = $this->service->getDefaultChargeData($params);
        return Common::success($data);
    }

    // 批量设置充值模板
    public function batchSetTemplate()
    {
        $params = Request::param();

        try {
            validate(\app\cps\validate\Video::class)->scene('batch_setting_template')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->batchSetRechargeTemplate($params);

        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }

        return Common::success($data);
    }

    // 渲染充值人均数据统计模板
    public function perCapita()
    {
        return view();
    }
    
  

    // 获取短剧的人均充值
    public function getVideoPerCapita()
    {
        $params = Request::param();

        $data = $this->service->getPerCapitaByVideo($params);

        return Common::success($data);
    }
}